<template>
    <show-modal
        :title="info && info.roleId ? '流程编辑' : '新增流程'"
        :loading="loading"
        width="800"
        @cancel="
            () => {
                $emit('cancel');
            }
        "
        :isSure="!isDisabled"
        @submit="submit"
    >
        <a-form-model :model="form" :rules="rules" v-bind="layout" ref="form">
            <!-- <a-form-model-item prop="flowName" label="流程名称">
                <a-input v-model.trim="form.flowName" :maxLength="20" :disabled="isDisabled" placeholder="请输入流程名称" />
            </a-form-model-item> -->
            <a-form-model-item prop="flowType" label="流程类型">
                <a-radio-group v-model="form.flowType">
                    <a-radio :value="item.value" v-for="item in flowTypes" :disabled="isDisabled" :key="item.value">{{item.label}}</a-radio>
                </a-radio-group>
            </a-form-model-item>
            <a-form-model-item prop="schemeType" v-if="form.flowType=='02'" label="类别">
                <a-radio-group v-model="form.schemeType">
                    <a-radio :value="item.value" v-for="item in schemeTypes" :disabled="isDisabled" :key="item.value">{{item.label}}</a-radio>
                </a-radio-group>
            </a-form-model-item>
            <a-form-model-item prop="deptId" label="所属项目部">
                <a-tree-select
                    v-model="form.deptId"
                    allowClear
                    style="width:100%"
                    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                    :tree-data="deptList"
                    :replaceFields="{children:'children',
                    title:'deptName',
                    key:'deptId',
                    value: 'deptId' }"
                     :disabled="isDisabled"
                    placeholder="请选择项目部"
                    @change="changeDept"
                >
                </a-tree-select>
            </a-form-model-item>
            <a-form-model-item label="状态" prop="status">
                <a-switch
                    :checked="form.status == '01' ? true : false"
                    checked-children="启用"
                    :disabled="isDisabled"
                    un-checked-children="未启用"
                    @change="onChangeShow" />
            </a-form-model-item>
            <template v-if="form.flowType=='01'">
                <a-form-model-item label="施工监督员" prop="sgjdyId">
                    <add-person ref="scheme" key="sgjdyId" :user="form.sgjdyId" :disabled="isDisabled" :deptList="deptList" :limit="1000" />
                </a-form-model-item>
                <a-form-model-item label="现场监察员" prop="xcjcyId">
                    <add-person ref="scene" key="xcjcyId" :user="form.xcjcyId" :disabled="isDisabled" :deptList="deptList" :limit="1000" />
                </a-form-model-item>
            </template>
            <template v-else>
                <a-form-model-item label="审批人1" prop="approver1Id">
                    <add-person ref="approve_one" key="approve_one" :user="form.approver1Id" :disabled="isDisabled" :deptList="deptList"/>
                </a-form-model-item>
                <a-form-model-item label="审批人2" prop="approver2Id">
                    <add-person ref="approve_two" key="approve_two" :user="form.approver2Id" :disabled="isDisabled" :deptList="deptList" />
                </a-form-model-item>
                <a-form-model-item label="审批人3" prop="approver3Id">
                    <add-person ref="approve_three" key="approve_three" :user="form.approver3Id" :disabled="isDisabled" :deptList="deptList" />
                </a-form-model-item>
                <a-form-model-item label="审批人4" prop="approver4Id" v-if="form.schemeType=='B' || form.schemeType=='A'">
                    <add-person ref="approve_four" key="approve_four" :user="form.approver4Id" :disabled="isDisabled" :deptList="deptList" />
                </a-form-model-item>
                <a-form-model-item label="审批人5" prop="approver5Id" v-if="form.schemeType=='A'">
                    <add-person ref="approve_five" key="approve_five" :user="form.approver5Id" :disabled="isDisabled" :deptList="deptList" />
                </a-form-model-item>
            </template>
        </a-form-model>
    </show-modal>
</template>

<script>
import { addInfo, updateInfo } from '@/api/system/worker';
import { rules } from './rules';
import addPerson from './add-person.vue';
import {flowTypes,schemeTypes,flowStatus} from '../dictionary'
import {deepClone} from '@/utils'

export default {
    name: 'roleModel',
    components:{
        addPerson
    },
    props: {
        info: {
            type: Object,
            default: () => {}
        },
        deptList:{
            type:Array,
            default:()=>[]
        }
    },
    computed: {
        isDisabled() {
            let {status=''} = this.info;
            return status=='01'?true:false;
        }
    },
    data() {
        return {
            rules,
            flowTypes,
            schemeTypes,
            flowStatus,
            form: {
                flowName:"",
                flowType:'01',
                deptId:undefined,
                deptName:'',
                status:'02',
                schemeType:'C',
                sgjdyId:"",//施工监督员id
                sgjdyName:'',
                xcjcyId:"",//现场监察员id
                xcjcyName:'',
                approver1Id:'',//审核员
                approver1Name:'',//审核员
                approver2Id:'',//审核员
                approver2Name:'',//审核员
                approver3Id:'',//审核员
                approver3Name:'',//审核员
                approver4Id:'',//审核员
                approver4Name:'',//审核员
                approver5Id:'',//审核员
                approver5Name:'',//审核员
            },
            loading: false,
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            }
        };
    },
    created() {
        this.form = { ...this.form, ...this.info };
    },
    methods: {
        //提交
        submit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        let form = deepClone(this.form);
                        let {flowType,schemeType} = form;
                        let res = null;
                        if(flowType=='01'){
                            //监管
                            let sgjdyData = this.$refs.scheme.list;
                            let xcjcyData = this.$refs.scene.list;
                            let {id:sgjdyId,name:sgjdyName} = this.dealPersonInfo(sgjdyData);
                            let {id:xcjcyId,name:xcjcyName} = this.dealPersonInfo(xcjcyData);
                            form.sgjdyId = sgjdyId;
                            form.sgjdyName = sgjdyName;
                            form.xcjcyId = xcjcyId;
                            form.xcjcyName = xcjcyName;
                            if(!form.sgjdyId){
                                this.$message.error('请选择施工监督员')
                                return false;
                            }
                            if(!form.xcjcyId){
                                this.$message.error('请选择现场监察员')
                                return false;
                            }
                        }else{
                            //计划审批
                            let approverOneData = this.$refs.approve_one.list
                            let approverTwoData = this.$refs.approve_two.list
                            let approverThreeData = this.$refs.approve_three.list;

                            let {id:approver1Id,name:approver1Name} = this.dealPersonInfo(approverOneData);
                            let {id:approver2Id,name:approver2Name} = this.dealPersonInfo(approverTwoData);
                            let {id:approver3Id,name:approver3Name} = this.dealPersonInfo(approverThreeData);
                           
                            form.approver1Id = approver1Id;
                            form.approver1Name = approver1Name;
                            form.approver2Id = approver2Id;
                            form.approver2Name = approver2Name;
                            form.approver3Id = approver3Id;
                            form.approver3Name = approver3Name;
                            
                            if(!form.approver1Id){
                                this.$message.error('请选择审批人1')
                                return false;
                            }
                            if(!form.approver2Id){
                                this.$message.error('请选择审批人2')
                                return false;
                            }
                            if(!form.approver3Id){
                                this.$message.error('请选择审批人3')
                                return false;
                            }

                            if(schemeType=='B' || schemeType=='A'){
                                let approverFourData = this.$refs.approve_four.list;
                                let {id:approver4Id,name:approver4Name} = this.dealPersonInfo(approverFourData);
                                form.approver4Id = approver4Id;
                                form.approver4Name = approver4Name;
                                if(!form.approver4Id){
                                    this.$message.error('请选择审批人4')
                                    return false;
                                }
                            }
                            if(schemeType=='A'){
                                let approverFiveData = this.$refs.approve_five.list;
                                let {id:approver5Id,name:approver5Name} = this.dealPersonInfo(approverFiveData);
                                form.approver5Id = approver5Id;
                                form.approver5Name = approver5Name;
                                if(!form.approver5Id){
                                    this.$message.error('请选择审批人5')
                                    return false;
                                }
                            }
                        }
                        if (form.id) {
                            //编辑
                            res = await updateInfo(form);
                        } else {
                            //新增addInfo
                            res = await addInfo(form);
                        }
                        let { code } = res;
                        this.$message.success('操作成功!');
                        this.$emit('submit');
                    } finally {
                        this.loading = false;
                    }
                }
            });
        },
        changeDept(value,label,extra){
            this.form.deptName = label[0] || '';
        },
        //将表格数据处理成需要的格式
        dealPersonInfo(data){
            let ids = [];
            let names = [];
            if(data && data.length){
                data.map(item=>{
                    ids.push(item.id);
                    names.push(item.name)
                })
            }
            let id = ids.join(',');
            let name = names.join(',')
            return {
                id,
                name
            }
        },
        //切换状态
        onChangeShow(checked) {
            if (checked) {
                this.form.status = '01';
            } else {
                this.form.status = '02';
            }
        },
    }
};
</script>
